package com.gin.kotlin.no_gay_alliance.activity

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import androidx.activity.result.ActivityResultLauncher
import androidx.core.view.MenuProvider
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.activity.MainActivity.Companion.handleCommonOptionsItemSelected
import com.gin.kotlin.no_gay_alliance.activity.PostActivity.Companion.obtainIntent
import com.gin.kotlin.no_gay_alliance.argument.PostPrepareArgument
import com.gin.kotlin.no_gay_alliance.argument.PostPrepareArgument.Action
import com.gin.kotlin.no_gay_alliance.argument.TopicListArgument
import com.gin.kotlin.no_gay_alliance.argument.TopicListArgument.Type
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingActivity
import com.gin.kotlin.no_gay_alliance.databinding.ActivityTopicListBinding
import com.gin.kotlin.no_gay_alliance.fragment.topiclist.AbstractTopicListFragment
import com.gin.kotlin.no_gay_alliance.fragment.topiclist.TopicPageFragment
import com.gin.retrofit.utils.JacksonUtils

/**
 * * 主题列表活动, 根据参数情况装载不同的Fragment
 * * 层级： [TopicListActivity] -> [AbstractTopicListFragment] -> [TopicPageFragment]
 */
class TopicListActivity : AbstractPagerListActivity<ActivityTopicListBinding>(){
    /**
     * 当前Fragment
     */
    private lateinit var currentFragment: AbstractTopicListFragment<*>

    /**
     * 页面参数
     */
    private lateinit var argument: TopicListArgument

    /**
     * 解析意图
     * @param intent Intent?
     */
    override fun handleIntent(intent: Intent) {
        this.argument = parseArgument(intent, KEY_ARGUMENT, TopicListArgument::class.java){ TopicListArgument.parseUrl(it)}
        updatePage(this.argument.page)
    }

    override fun onResume() {
        super.onResume()
        // 设置临时标题
        this.argument.title?.also { setToolbarTitle(it) }
    }


    /**
     * 装载右上角菜单, 使用 [MenuInflater.inflate] 方法
     * @param menu Menu
     * @param menuInflater MenuInflater
     */
    override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
        //清空菜单
        menu.clear()

        //先装载当前Fragment的菜单
        (currentFragment as? MenuProvider)?.onCreateMenu(menu, menuInflater)
    }

    override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
        menu?.also { (currentFragment as? MenuProvider)?.onPrepareMenu(it) }
        return super.onPrepareOptionsMenu(menu)
    }

    /**
     * 菜单响应
     * @param item MenuItem
     * @return Boolean
     */
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // 先由Fragment处理
        if ((currentFragment as? MenuProvider)?.onMenuItemSelected(item) == true) {
            return true
        }
        return handleCommonOptionsItemSelected(this, handler, item)
    }


    /**
     * onCreate方法的后续操作，如 [AbstractBindingActivity.registerLauncher]， 或 [AbstractBindingActivity.attachTabLayout]
     */
    override fun onCreate() {
        // 初始化Fragment，替换当前content
        this.currentFragment = argument.type.newInstance.invoke(this.argument)
        supportFragmentManager.beginTransaction().replace(R.id.fragment, this.currentFragment).commit()

        val b = this.b ?: return

        //  设置浮动按钮可见性
        b.floatButtonPost.visibility = when (this.argument.type) {
            Type.FORUM_LIST, Type.COL_LIST -> View.VISIBLE
            else -> View.GONE
        }
    }

    override fun refresh() {
        currentFragment.refresh()
    }

    override fun launchPostActivity(launcher: ActivityResultLauncher<Intent>) {
        launcher.launch(obtainIntent(this, PostPrepareArgument(Action.NEW, forumId = argument.forumId, colTid = argument.colTid)))
    }

    override fun handlePostResult(postResult: PostActivity.PostResult?) {
        Log.i(TAG, "发帖结果")
        postResult?.also { JacksonUtils.println(it) }
    }

    companion object {
        const val KEY_ARGUMENT = "ARGUMENT_TOPIC_LIST"

        /**
         * 使用参数启动活动
         * @param context Context
         * @param argument TopicListArgument
         */
        fun startActivity(context: Context, argument: TopicListArgument) {
            context.startActivity(Intent(context, TopicListActivity::class.java).also { it.putExtra(KEY_ARGUMENT, argument) })
        }

        /**
         * 使用URL启动活动
         * @param context Context
         * @param url String
         */
        fun startActivity(context: Context, url: String) {
            context.startActivity(Intent(context, TopicListActivity::class.java).also { it.setData(Uri.parse(url)) })
        }
    }

}